﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace Activity.Helper
{
  public  class CommHelper
    {
        //泛型类
        public class ArrayItem<T>
        {
            T[] item;
            //构造函数
            public ArrayItem(T[] obj)
            {
                item = new T[obj.Length];
                for (int i = 0; i < obj.Length; i++)
                {
                    item[i] = obj[i];
                }
            }
            public Type ShowType() { return typeof(T); } //返回类型
            public T[] GetItems() { return item; } //返回原数组
                                                   //返回打乱顺序后的数组
            public T[] GetDisruptedItems()
            {
                //生成一个新数组：用于在之上计算和返回
                T[] temp;
                temp = new T[item.Length];
                for (int i = 0; i < temp.Length; i++) { temp[i] = item[i]; }
                //打乱数组中元素顺序
                Random rand = new Random(DateTime.Now.Millisecond);
                for (int i = 0; i < temp.Length; i++)
                {
                    int x, y; T t;
                    x = rand.Next(0, temp.Length);
                    do
                    {
                        y = rand.Next(0, temp.Length);
                    } while (y == x);
                    t = temp[x];
                    temp[x] = temp[y];
                    temp[y] = t;
                }
                return temp;
            }
        }

        /// <summary>
        /// DataTable转List
        /// </summary>
        /// <typeparam name="T"></typeparam>
        public class ModelConvertHelper<T> where T : new()
        {
            /// <summary>
            /// 
            /// </summary>
            /// <param name="dt"></param>
            /// <returns></returns>
            public static IList<T> ConvertToModel(DataTable dt)
            {
                IList<T> ts = new List<T>();// 定义集合
                try
                {
                    foreach (DataRow dr in dt.Rows)
                    {
                        var t = new T();
                        var propertys = t.GetType().GetProperties(); // 获得此模型的公共属性
                        foreach (var pi in propertys)
                        {
                            var tempName = pi.Name;
                            if (!dt.Columns.Contains(tempName)) continue;
                            if (!pi.CanWrite) continue;
                            var value = dr[tempName];
                            if (value == DBNull.Value) continue;
                            if (pi.GetMethod.ReturnParameter != null && pi.GetMethod.ReturnParameter.ParameterType.Name == "Int32")
                            {
                                value = Convert.ToInt32(value);
                            }
                            pi.SetValue(t, value, null);
                        }
                        ts.Add(t);
                    }
                }
                catch (Exception ex)
                {
                    Log.Error(ex);
                    throw (ex);
                }
                return ts;
            }
            }

        public static class DataTableHelper
        {
            /// <summary>
            /// 将IEnumerable<T>类型的集合转换为DataTable类型 
            /// </summary>
            /// <typeparam name="T"></typeparam>
            /// <param name="varlist"></param>
            /// <returns></returns>
            public static DataTable LinqToDataTable<T>(IEnumerable<T> varlist)
            {   //定义要返回的DataTable对象
                var dtReturn = new DataTable();
                // 保存列集合的属性信息数组
                PropertyInfo[] oProps = null;
                if (varlist == null) return dtReturn;//安全性检查
                //循环遍历集合，使用反射获取类型的属性信息
                foreach (T rec in varlist)
                {
                    //使用反射获取T类型的属性信息，返回一个PropertyInfo类型的集合
                    if (oProps == null)
                    {
                        oProps = ((Type)rec.GetType()).GetProperties();
                        //循环PropertyInfo数组
                        foreach (var pi in oProps)
                        {
                            var colType = pi.PropertyType;//得到属性的类型
                            //如果属性为泛型类型
                            if ((colType.IsGenericType) && (colType.GetGenericTypeDefinition()
                                                            == typeof(Nullable<>)))
                            {   //获取泛型类型的参数
                                colType = colType.GetGenericArguments()[0];
                            }
                            //将类型的属性名称与属性类型作为DataTable的列数据
                            dtReturn.Columns.Add(new DataColumn(pi.Name, colType));
                        }
                    }
                    //新建一个用于添加到DataTable中的DataRow对象
                    var dr = dtReturn.NewRow();
                    //循环遍历属性集合
                    foreach (PropertyInfo pi in oProps)
                    {   //为DataRow中的指定列赋值
                        dr[pi.Name] = pi.GetValue(rec, null) == null ?
                            DBNull.Value : pi.GetValue(rec, null);
                    }
                    //将具有结果值的DataRow添加到DataTable集合中
                    dtReturn.Rows.Add(dr);
                }
                return dtReturn;//返回DataTable对象
            }
        }

        /// <summary>
        /// 获取查询店铺控件传入的shopid,添加单引号和圆括号
        /// </summary>
        /// <remarks>
        /// eg.  KM000026,KM000055,KM000120
        /// 返回 ('KM000026','KM000055','KM000120')
        /// </remarks>
        /// <param name="ShopidShopIDs">eg.  KM000026,KM000055,KM000120</param>
        /// <returns>处理后的string,('KM000026','KM000055','KM000120')</returns>
        public static string GetString(string ShopidShopIDs)
        {
            StringBuilder result = new StringBuilder();
            if (!string.IsNullOrWhiteSpace(ShopidShopIDs))
            {
                result.Append("(");
                string[] str = ShopidShopIDs.Split(',');
                for (var index = 0; index < str.Length; index++)
                {
                    result.Append("'");
                    result.Append(str[index]);
                    result.Append("'");
                    if (index + 1 != str.Length)
                    {
                        result.Append(",");
                    }
                }
                result.Append(")");
            }
            return result.ToString();
        }
    }

}
